package mt.demo.web.controller.system;

import mt.demo.domain.system.SysFunctionDomain;
import mt.demo.domain.user.UserRoleDomain;
import mt.demo.service.comm.YomsException;
import mt.demo.service.system.SysFunctionService;
import mt.demo.service.user.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * Created by watson on 2015/12/16.
 */
@Controller
public class PermissionController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private SysFunctionService sysFunctionService;

    //访问所有权限和角色页面
    @RequestMapping(value = "permission.html", method = RequestMethod.GET)
    public ModelAndView showRoleAndSysFunction() throws YomsException {
        List<SysFunctionDomain> sysDomains = sysFunctionService.getAllSysFunction(null);
        List<UserRoleDomain> roleDomains = roleService.getAllRoleDomain(null);
        ModelAndView mav = new ModelAndView("user/permission");
        mav.addObject("sysDomains", sysDomains);
        mav.addObject("roleDomains", roleDomains);
        return mav;
    }

    //给角色分配权限
    @RequestMapping(value = "allotFunToRole.html", method = RequestMethod.GET)
    @ResponseBody
    public void allotFuncToRole(int roleId, int funId) throws YomsException {
        if (roleId == 0 || funId == 0) {
            throw new YomsException("传入参数为空");
        } else {
            roleService.allotFunToRole(roleId, funId);
        }
    }

    //取消角色权限
    @RequestMapping(value = "cancelFunToRole.html", method = RequestMethod.GET)
    @ResponseBody
    public void cancelFuncToRole(int roleId, int funId) throws YomsException {
        if (roleId == 0 || funId == 0) {
            throw new YomsException("传入参数为空");
        } else {
            roleService.cancelFunToRole(roleId, funId);
        }
    }
}
